<template>
  <!-- 新增部门的弹层 -->
  <el-dialog title="新增部门" :visible="showDialog">
    <!-- 表单组件  el-form   label-width设置label的宽度   -->
    <!-- 匿名插槽 -->
    <el-form label-width="120px" :model="formData" :rules="rules">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="formData.name" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="formData.code" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="formData.manager" style="width:80%" placeholder="请选择" />
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="formData.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
      </el-form-item>
    </el-form>
    <!-- el-dialog有专门放置底部操作栏的 插槽  具名插槽 -->
    <el-row slot="footer" type="flex" justify="center">
      <!-- 列被分为24 -->
      <el-col :span="6">
        <el-button type="primary" size="small">确定</el-button>
        <el-button size="small">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>
<script>
import { getDepartments } from '@/api/departments'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
    // node: {
    //   type: Object,
    //   default() {
    //     return {}
    //   }
    // }
  },
  data() {
    const checkRepeatName = async(rule, val, callback) => {
      // 1. 获取全部的部门列表
      const { depts } = await getDepartments()
      // 2. 筛选出当前被点击的部门下属(跟正在创建同级)
      const isRepeat = depts
        .filter(item => item.pid === this.node.id)
      // 3. 判断是否有同名的元素
        .some(item => item.name === val)

      // 4. 处理后续, 有重名报错, 没有就放行
      isRepeat ? callback(new Error('同一部门下不能重名')) : callback()
    }

    const checkRepeatCode = async(rule, val, callback) => {
      // 1. 获取全部部门
      const { depts } = await getDepartments()
      // 2. 判断是否有相同 code
      const isRepeat = depts.some(item => item.code === val)
      // 3. 处理后续
      isRepeat ? callback(new Error('部门编码必须唯一')) : callback()
    }
    return {
      formData: {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      },
      rules: {
        name: [
          { required: true, message: '数据不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '长度在 1-50 之间', trigger: 'blur' },
          { trigger: 'blur', validator: checkRepeatName }
        ],
        code: [
          { required: true, message: '数据不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '长度在 1-50 之间', trigger: 'blur' },
          { trigger: 'blur', validator: checkRepeatCode }
        ],
        manager: [
          { required: true, message: '数据不能为空', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '数据不能为空', trigger: 'blur' },
          { min: 1, max: 300, message: '长度在 1-300 之间', trigger: 'blur' }
        ]
      }
    }
  }
}
</script>

